package exercise;

import java.util.HashMap;
import java.util.Map;

class Solution10 {
    public int lengthOfLongestSubstring(String s) {
        Map<Character, Integer> map = new HashMap<>();
        int len = s.length();
        int l = 0, r = 0, ans = 0;
        while (r < len) {
            char ch = s.charAt(r);
            if (map.getOrDefault(ch, 0) == 0) {
                map.put(ch, 1);
                ans = Math.max(ans, r-l+1);
                // System.out.println(ans);
                r++;
            } else {
                map.put(s.charAt(l), map.get(s.charAt(l))-1);
                l++;
            }
        }
        return ans;
    }
}